package com.min.quartz.demo01;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import java.text.ParseException;
import  java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] args) throws SchedulerException, InterruptedException, ParseException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        TriggerBuilder triggerBuilder;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND,5);
        //方式1,创建普通触发器
        Trigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("job1","group1")
                .startAt(calendar.getTime())
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(6)
                        .withRepeatCount(2)
                ).build();
        //方式2创建cron触发器

        Trigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("cron01", "crongroup01")
                .withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ? *"))
                .build();

//        CronTriggerImpl cronTrigger = new CronTriggerImpl();
//        cronTrigger.setName("cron01");
//        cronTrigger.setGroup("crongroup01");
//        //每2秒钟执行
//        cronTrigger.setCronExpression("*/2 * * * * ? *");
        //创建任务
        JobDetail jobDetail1 = JobBuilder.newJob(MyJob01.class)
                .withIdentity("myjob1", "group2")
                .build();
        JobDetail jobDetail2 = JobBuilder.newJob(MyJob01.class)
                .withIdentity("myjob2", "group1")
                .build();


        //把任务和触发器提交给调度器
        scheduler.scheduleJob(jobDetail1, cronTrigger);
        scheduler.scheduleJob(jobDetail2, simpleTrigger);
        //开始调度
        scheduler.start();

        Thread.sleep(10000);
        //关闭调度
        scheduler.shutdown();

    }
}
